<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis 延迟加载深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
            100% { transform: translateY(0px); }
        }
        
        .floating {
            animation: float 6s ease-in-out infinite;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    MyBatis <span class="text-yellow-300">延迟加载</span>
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    优雅的性能优化策略，让数据加载更智能
                </p>
                <div class="flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>按需加载
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-bolt mr-2"></i>性能优化
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-code mr-2"></i>代理模式
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">M</span>yBatis 的延迟加载（Lazy Loading）是一种优化策略，用于在需要时才加载关联对象的数据，而不是在查询主对象时就立即加载所有关联对象的数据。这可以减少查询时的数据量，提高系统的性能和响应速度。延迟加载可以分为两种类型：单条延迟加载和关联集合延迟加载。
            </p>
        </div>

        <!-- Types of Lazy Loading -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card rounded-2xl p-8">
                <div class="feature-icon">
                    <i class="fas fa-user"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">单条延迟加载</h3>
                <p class="text-gray-600 leading-relaxed">
                    单条延迟加载是指延迟加载一个关联对象的数据。当查询主对象时，关联对象的数据并不会立即加载，只有在真正访问关联对象的数据时才会触发额外的 SQL 查询来加载这个关联对象的数据。
                </p>
            </div>
            
            <div class="content-card rounded-2xl p-8">
                <div class="feature-icon">
                    <i class="fas fa-users"></i>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">关联集合延迟加载</h3>
                <p class="text-gray-600 leading-relaxed">
                    关联集合延迟加载是指延迟加载一个关联对象的集合数据。当查询主对象时，关联对象集合的数据并不会立即加载，只有在真正访问关联对象集合的数据时才会触发额外的 SQL 查询来加载这个关联对象集合的数据。
                </p>
            </div>
        </div>

        <!-- Implementation Principle -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>实现原理
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                MyBatis 实现延迟加载的原理主要基于代理模式和动态代理的机制。在 MyBatis 中，可以通过配置 XML 映射文件或者注解来指定延迟加载的策略。一般情况下，延迟加载需要满足以下条件：
            </p>
            <div class="bg-purple-50 rounded-xl p-6">
                <ul class="space-y-3">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                        <span class="text-gray-700">关联对象需要被指定为延迟加载。</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                        <span class="text-gray-700">关联对象必须是可序列化的，以便在延迟加载时可以将关联对象传输到新的 Session 上下文中。</span>
                    </li>
                </ul>
            </div>
            <p class="text-gray-700 mt-6">
                在配置延迟加载时，可以使用 MyBatis 提供的一些配置选项，例如 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">lazyLoadingEnabled</code> 和 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">aggressiveLazyLoading</code> 等，来控制延迟加载的行为。
            </p>
        </div>

        <!-- Visualization -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>延迟加载流程图
            </h2>
            <div class="mermaid">
                graph TD
                    A[查询主对象] --> B{是否开启延迟加载?}
                    B -->|是| C[只查询主对象数据]
                    B -->|否| D[立即查询所有关联数据]
                    C --> E{访问关联对象?}
                    E -->|是| F[触发SQL查询关联数据]
                    E -->|否| G[不查询关联数据]
                    F --> H[返回完整数据]
                    G --> I[只返回主对象数据]
                    D --> H
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#fa709a,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Example Section -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">
                <i class="fas fa-code mr-3 text-purple-600"></i>实战案例
            </h2>
            
            <!-- Dept.java -->
            <div class="mb-8">
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Dept.java
                    </div>
                    <pre class="p-6 overflow-x-auto"><code class="text-sm">private Integer id;
private String name;
private String location;
private List&lt;Emp&gt; emps; // 一个部门中会存在多个员工</code></pre>
                </div>
            </div>

            <!-- DepartmentDao.xml -->
            <div class="mb-8">
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-file-code mr-2"></i>DepartmentDao.xml
                    </div>
                    <pre class="p-6 overflow-x-auto"><code class="text-sm">&lt;mapper namespace="com.ts.mybatis.part2.one2many.DepartmentDao"&gt;
  
    &lt;resultMap id="departmentResultMap" type="department"&gt;
        &lt;id property="id" column="id" /&gt;
        &lt;result property="name" column="name" /&gt;
        &lt;result property="location" column="location" /&gt;
      
         &lt;!-- column="传入目标方法的条件参数"  select="及联调用的查询目标"--&gt;
        &lt;collection property="emps" ofType="Employee" column="id"
                    select="com.qf.mybatis.part2.one2many.EmployeeDao.selectEmployeeByDeptId" /&gt;
    &lt;/resultMap&gt;
  
    &lt;select id="selectAllDepartments" resultMap="departmentResultMap"&gt;
        SELECT id , name , location
        FROM t_departments
        WHERE id = #{id}
    &lt;/select&gt;
&lt;/mapper&gt;</code></pre>
                </div>
            </div>

            <!-- EmployeeDao.xml -->
            <div class="mb-8">
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-file-code mr-2"></i>EmployeeDao.xml
                    </div